---
title: Spec - howl.completion.api_completer
tags: spec
---
<div class="spec-group spec-group-1">

<h1 id="howl.completion.api_completer">howl.completion.api_completer</h1>

<pre class="highlight moonscript"><code><span class="n">factory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">completion</span><span class="p">.</span><span class="n">api</span><span class="p">.</span><span class="n">factory</span></code></pre>

<div class="spec-group spec-group-2">

<h2 id="complete()">complete()</h2>

<pre class="highlight moonscript"><code><span class="n">api</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="ss">keyword:</span><span class="w"> </span><span class="p">{},</span><span class="w">
  </span><span class="ss">function:</span><span class="w"> </span><span class="p">{},</span><span class="w">
  </span><span class="ss">sub:</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="ss">foo:</span><span class="w"> </span><span class="p">{}</span><span class="w">
    </span><span class="ss">bar:</span><span class="w"> </span><span class="p">{}</span><span class="w">
    </span><span class="ss">zed:</span><span class="w"> </span><span class="p">{</span><span class="w">
      </span><span class="ss">frob:</span><span class="w"> </span><span class="p">{}</span><span class="w">
      </span><span class="ss">other:</span><span class="w"> </span><span class="p">{}</span><span class="w">
    </span><span class="p">}</span><span class="w">
  </span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="kd">local</span><span class="w"> </span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="n">mode</span><span class="w">

</span><span class="n">complete_at</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">pos</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">context</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="o">\</span><span class="n">context_at</span><span class="w"> </span><span class="n">pos</span><span class="w">
  </span><span class="n">completer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">factory</span><span class="w"> </span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="n">context</span><span class="w">
  </span><span class="n">comps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">completer</span><span class="o">\</span><span class="n">complete</span><span class="w"> </span><span class="n">context</span><span class="w">
  </span><span class="nb">table.sort</span><span class="w"> </span><span class="n">comps</span><span class="w">
  </span><span class="n">comps</span><span class="w">

</span><span class="n">before_each</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">DefaultMode</span><span class="o">!</span><span class="w">
  </span><span class="n">mode</span><span class="p">.</span><span class="n">api</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">api</span><span class="w">
  </span><span class="n">mode</span><span class="p">.</span><span class="n">completers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'api'</span><span class="w"> </span><span class="p">}</span><span class="w">
  </span><span class="n">buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="n">mode</span></code></pre>


<h4 id="returns-global-completions-when-no-prefix-is-found">returns global completions when no prefix is found</h4>

<pre class="highlight moonscript"><code><span class="n">buffer</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">' k\nfun'</span><span class="w">
</span><span class="n">comps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="n">buffer</span><span class="p">.</span><span class="n">lines</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="n">start_pos</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'function'</span><span class="p">,</span><span class="w"> </span><span class="s1">'keyword'</span><span class="p">,</span><span class="w"> </span><span class="s1">'sub'</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">comps</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'function'</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="n">buffer</span><span class="p">.</span><span class="n">lines</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="n">end_pos</span></code></pre>


<h4 id="returns-authoritive-scoped-completions-when-appropriate">returns authoritive scoped completions when appropriate</h4>

<pre class="highlight moonscript"><code><span class="n">buffer</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'sub.zed:f'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">,</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="s1">'zed'</span><span class="p">,</span><span class="w"> </span><span class="ss">authoritive:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="mi">5</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'zed'</span><span class="p">,</span><span class="w"> </span><span class="ss">authoritive:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="mi">6</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'frob'</span><span class="p">,</span><span class="w"> </span><span class="s1">'other'</span><span class="p">,</span><span class="w"> </span><span class="ss">authoritive:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="mi">9</span></code></pre>


<h4 id="returns-an-empty-set-for-non-matched-prefixes">returns an empty set for non-matched prefixes</h4>

<pre class="highlight moonscript"><code><span class="n">buffer</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'well so.sub'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="mi">5</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="mi">8</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="n">buffer</span><span class="p">.</span><span class="n">length</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="(when-mode-provides-a-.resolve_type()-method)">(when mode provides a .resolve_type() method)</h3>

<h4 id="is-invoked-with-(mode,-context)">is invoked with (mode, context)</h4>

<pre class="highlight moonscript"><code><span class="n">mode</span><span class="p">.</span><span class="n">resolve_type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">spy</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="kc">nil</span><span class="w">
</span><span class="n">buffer</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'lookie'</span><span class="w">
</span><span class="n">complete_at</span><span class="w"> </span><span class="mi">5</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">mode</span><span class="p">.</span><span class="n">resolve_type</span><span class="p">).</span><span class="n">was_called_with</span><span class="w"> </span><span class="n">match</span><span class="p">.</span><span class="n">is_ref</span><span class="p">(</span><span class="n">mode</span><span class="p">),</span><span class="w"> </span><span class="n">buffer</span><span class="o">\</span><span class="n">context_at</span><span class="w"> </span><span class="mi">5</span></code></pre>


<h4 id="the-returned-(path,-part)-is-used-for-looking-up-completions">the returned (path, part) is used for looking up completions</h4>

<pre class="highlight moonscript"><code><span class="n">mode</span><span class="p">.</span><span class="n">resolve_type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="s1">'sub'</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="s1">'sub'</span><span class="p">}</span><span class="w">
</span><span class="n">buffer</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'look.'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">,</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="s1">'zed'</span><span class="p">,</span><span class="w"> </span><span class="ss">authoritive:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">complete_at</span><span class="w"> </span><span class="mi">6</span></code></pre>

</div>
</div>
</div>
